from entity.User import User
from util import dbUtil

currentUser: User = None


def login(user: User):
    """
    用户登录判断
    :param user: 用户实体
    :return: 登录成功返回用户信息实体，失败返回None
    """
    conn = None
    try:
        conn = dbUtil.get_db_conn()
        cursor = conn.cursor()
        sql = 'select * from cq_user where 用户名=%s and 密码=%s'
        cursor.execute(sql, (user.username, user.password))
        print(cursor.mogrify(sql, (user.username, user.password)))
        return cursor.fetchone()
    except Exception as e:
        if conn:
            conn.rollback()
        print(e)
        return None
    finally:
        if conn:
            dbUtil.close_db_conn(conn)
        print('连接已关闭')
